//
//  GYShopingCartHeadView.m
//  GoodLuck
//
//  Created by apple on 2018/3/27.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *headViewIdentifier = @"headViewIdentifier";

#import "GYShopingCartHeadView.h"
#import "UILabel+GYExtension.h"

@interface GYShopingCartHeadView ()

/** 店铺名字 */
@property (nonatomic, strong) UILabel *storeNameLable;

/** 选中按钮 */
@property (nonatomic, strong) UIButton *statesBtu;;
@end
@implementation GYShopingCartHeadView

+ (instancetype)headViewWithTbaleView:(UITableView *)tableView {
    GYShopingCartHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headViewIdentifier];
    if (headView == nil) {
        headView = [[GYShopingCartHeadView alloc] initWithReuseIdentifier:headViewIdentifier];
    }
    
    return headView;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
   self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        //初始化
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    self.contentView.backgroundColor = [UIColor whiteColor];
    //1.创建子控件
    UIButton *stateBtu = [self creatBtu:@"weixuanzhong" selImage:@"xuanzhong"];
    [stateBtu addTarget:self action:@selector(stateBtuClick) forControlEvents:UIControlEventTouchUpInside];
    self.statesBtu = stateBtu;
    
    UIButton *storeBtu = [self creatBtu:@"store_b" selImage:@"store_b"];
    UILabel *storeLable = [UILabel layoutWith:@"" line:1 textColor:[UIColor blackColor]];
    [self.contentView addSubview:storeLable];
    self.storeNameLable = storeLable;
    
    UILabel *markLable = [UILabel layoutLbale:RGBColor(245, 245, 245)];
    [self.contentView addSubview:markLable];
    
    //2.添加子控件
    NSDictionary *dic = @{@"stateBtu":stateBtu,@"storeBtu":storeBtu,@"storeLable":storeLable,@"markLable":markLable};
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[stateBtu(20)]" options:0 metrics:nil views:dic]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[stateBtu(20)]" options:0 metrics:nil views:dic]];
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[stateBtu]-10-[storeBtu(20)]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:dic]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[storeBtu(20)]" options:0 metrics:nil views:dic]];
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[storeBtu]-10-[storeLable]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:dic]];
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[markLable]-0-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:dic]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[markLable(1)]" options:0 metrics:nil views:dic]];
}

- (void)setModel:(GYShopingCart *)model {
    _model = model;
    
    self.storeNameLable.text = model.store_name;
    self.statesBtu.selected = model.isSelectState;
}

- (void)stateBtuClick {
    NSLog(@"头视图状态按钮的选择....");
    self.updateSelectStateCallBack();
}

- (UIButton *)creatBtu:(NSString *)norImage selImage:(NSString *)selImage {
    UIButton *btu = [[UIButton alloc] init];
    [btu setBackgroundImage:[UIImage imageNamed:norImage] forState:UIControlStateNormal];
    [btu setBackgroundImage:[UIImage imageNamed:selImage] forState:UIControlStateSelected];
    btu.translatesAutoresizingMaskIntoConstraints = false;
    [self.contentView addSubview:btu];

    return btu;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
